<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('商家产品')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row" style="height: 60px;">
        <div class="col-sm-12 search-collapse">
            <div class="col-sm-2">
                <span>已上架(38)</span>
            </div>
            <div class="col-sm-2">
                <span>未上架(38)</span>
            </div>
            <div class="col-sm-2">
                <span>已下架(38)</span>
            </div>
            <div class="col-sm-2">
                <span>审核中(38)</span>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <p>订单ID：</p>
                            <input type="text" name="orderId"/>
                        </li>
                        <li>
                            <p>用户ID：</p>
                            <input type="text" name="userId"/>
                        </li>
                        <li>
                            <p>类型：</p>
                            <select name="type">
                                <option value="">所有</option>
                                <option value="2">已上架</option>
                                <option value="3">未上架</option>
                                <option value="3">已下架</option>
                                <option value="3">审核中</option>
                            </select>
                        </li>
                        <li>
                            <p>店铺ID：</p>
                            <input type="text" name="storeId"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>

<!-- 填写单号Modal-->
<div class="modal inmodal" id="fillInTheNumberModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content animated">
            <div class="modal-header">
                <button type="button" onclick="closeFillInTheNumberModal()" class="close" data-dismiss="modal"><span
                        aria-hidden="true">&times;</span><span
                        class="sr-only" onclick="closeModal()">关闭</span>
                </button>
                <span class="modal-title">填写单号</span>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label>快递公司</label>
                    <input type="hidden" placeholder="请输入快递公司" class="form-control" id="companyCode">
                </div>
                <div class="form-group">
                    <label>快递单号</label>
                    <input type="text" placeholder="请输入快递单号" class="form-control" id="companyNo">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal" onclick="closeFillInTheNumberModal()">
                    取消
                </button>
                <button type="button" class="btn btn-primary" onclick="submitFillInTheNumber()">确定</button>
            </div>
        </div>
    </div>
</div>

<!-- 审核 Modal-->
<div class="modal inmodal" id="auditModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" onclick="closeAuditModal()">
                    <span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                </button>
                <h4 class="modal-title">审核</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <input type="hidden" id="aftersalesId" value="">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">退换货类型：</label>
                        <div class="col-sm-9">
                            <label class="radio-inline">
                                <input type="radio" name="aftersalesType" id="aftersalesType1" value="1"> 退货
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="aftersalesType" id="aftersalesType2" value="2"> 换货
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">审核结果：</label>
                        <div class="col-sm-9">
                            <label class="radio-inline">
                                <input type="radio" name="auditType" id="auditType1" value="1"> 通过
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="auditType" id="auditType2" value="2"> 拒绝
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">备注：</label>
                        <div class="col-sm-9">
                            <textarea name="remark" cols="10" rows="4" class="form-control" id="remark"
                                      placeholder="请输入备注">
                            </textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal" onclick="closeAuditModal()">取消</button>
                <button type="button" class="btn btn-primary" onclick="submitAudit()">确定</button>
            </div>
        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('bill:aftersales:edit')}]];
    var removeFlag = [[${@permission.hasPermi('bill:aftersales:remove')}]];
    var prefix = ctx + "bill/aftersales";
    var options = {
        url: prefix + "/list",
        createUrl: prefix + "/add",
        updateUrl: prefix + "/edit/{id}",
        removeUrl: prefix + "/remove",
        exportUrl: prefix + "/export",
        modalName: "退货单",
        columns: [{
            checkbox: true
        },
            {
                field: 'aftersalesId',
                title: '售后单id',
                visible: true
            },
            {
                field: 'orderId',
                title: '关联订单'
            },
            {
                field: 'aftersalesSn',
                title: '服务单号'
            },
            {
                field: 'type',
                title: '退换货类型',
                formatter: function (value, row, index) {
                    if (row.type === 2) {
                        return '<span class="btn btn-success">退货退款</span>';
                    } else if (row.type === 3) {
                        return '<span class="btn btn-primary">换货不退款</span>';
                    }
                }
            },
            {
                field: 'ctime',
                title: '申请时间'
            },
            {
                field: 'type',
                title: '用户ID'
            },
            {
                field: 'refund',
                title: '退款金额'
            },
            {
                field: 'aftersalesStatus',
                title: '售后状态',
                formatter: function (value, row, index) {
                    if (row.aftersalesStatus === 0) {//0待平台审核 1平台已拒绝  2待买家发货 3买家已发货，待商家收货 4商家已收货  5商家已发货 6买家已收货(换货完成)
                        return '待平台审核 ';
                    } else if (row.aftersalesStatus === 1) {
                        return '平台已拒绝  ';
                    } else if (row.aftersalesStatus === 2) {
                        return '待买家发货 ';
                    } else if (row.aftersalesStatus === 3) {
                        return '买家已发货，待商家收货 ';
                    } else if (row.aftersalesStatus === 4) {
                        return '商家已收货 ';
                    } else if (row.aftersalesStatus === 5) {
                        return '商家已发货 ';
                    } else if (row.aftersalesStatus === 6) {
                        return '买家已收货(换货完成) ';
                    }
                }
            },
            {
                field: 'refundStatus',
                title: '退款状态',
                formatter: function (value, row, index) {
                    if (row.refundStatus === 1) {//状态 1=未退款 2=已退款 3=退款失败，可以再次退款，4退款拒绝
                        return '未退款 ';
                    } else if (row.refundStatus === 2) {
                        return '已退款 ';
                    } else if (row.refundStatus === 3) {
                        return '退款失败 ';
                    } else if (row.refundStatus === 4) {
                        return '退款拒绝 ';
                    } else {
                        return '未知状态 ';
                    }
                }
            },
            {
                field: 'status',
                title: '处理状态',
                formatter: function (value, row, index) {
                    if (row.status === 0) {// 状态 0待处理 1退换货中 2已完成  3已拒绝
                        return '待处理 ';
                    } else if (row.status === 1) {
                        return '退换货中 ';
                    } else if (row.status === 2) {
                        return '已完成 ';
                    } else if (row.status === 3) {
                        return '已拒绝 ';
                    }
                }
            },
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var actions = [];
                    actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="detail(\'' + row.aftersalesId + '\')">查看详情</a> ');

                    if (row.status === 0) {
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="audit(\'' + row.aftersalesId + '\')">审核</a>');
                    }

                    // 退货退款、退换货中、商家已收货
                    if (row.refundStatus === 1 && row.type === 2 && row.status === 1 && row.aftersalesStatus === 4) {
                        actions.push('<a class="btn btn-warning btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="refundAccmount(\'' + row.aftersalesId + '\')">退款</a>');
                    }
                    // // 退货退款、退换货中、商家已收货
                    // if (row.refundStatus === 1 && row.type === 2 && row.status === 1 && row.aftersalesStatus === 4) {
                    //     actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="fillInTheNumber(\'' + row.aftersalesId + '\')">退款</a>');
                    // }
                    // 换货、退换货中、商家已收货
                    if (row.type === 3 && row.status === 1 && row.aftersalesStatus === 4) {
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="fillInTheNumber(\'' + row.aftersalesId + '\')">填写单号</a>');
                    }
                    return actions.join('');
                }
            }]
    };
    $(function () {
        $.table.init(options);
    });

    /**
     * 查看详情
     */
    function detail(id) {
        $.modal.open("查看详情", "/bill/aftersales/detail?id=" + id);
    }

    /**
     * 显示填写单号
     */
    function fillInTheNumber(id) {
        $("#aftersalesId").val(id);
        $("#fillInTheNumberModal").show();
    }

    /**
     * 取消填写单号
     */
    function closeFillInTheNumberModal() {
        $("#fillInTheNumberModal").hide();
    }

    /**
     * 提交确认单号
     */
    function submitFillInTheNumber() {
        // alert("填写单号！")
        var sellerLogiCode = $("#companyCode").val();
        var sellerLogiNo = $("#companyNo").val();
        // 调用接口
        $.ajax({
            type: "POST",
            url: ctx + "bill/aftersales/handleDeliverGoods",
            data: {
                "sellerLogiCode": sellerLogiCode,
                "sellerLogiNo": sellerLogiNo
            },
            datatype: "json",
            success: function (jsonResult) {
                if (jsonResult.code == 0) {
                    // 发货成功
                    $.modal.alertSuccess("发货成功");
                    $("#fillInTheNumberModal").hide();
                    // 刷新
                    location.reload();
                } else {
                    $.modal.alertWarning(jsonResult.msg);
                }
            },
            error: function (data) {
                $.modal.alertWarning("发货失败");
            }
        });
    }

    /**
     * 取消审核
     */
    function closeAuditModal() {
        $("#auditModal").hide();
    }

    /**
     * 显示审核
     * @param id
     */
    function audit(id) {
        $("#aftersalesId").val(id);
        $("#auditModal").show();
    }

    /**
     * 退款
     * @param id
     */
    function refundAccmount(id) {
        // 调用接口
        $.ajax({
            type: "POST",
            url: ctx + "bill/aftersales/refundAccmount",
            datatype: "json",
            data: {
                "aftersalesId": id
            },
            success: function (jsonResult) {
                if (jsonResult.code == 0) {
                    // 退款成功
                    $.modal.alertSuccess("退款成功");
                    // 刷新
                    location.reload();
                } else {
                    $.modal.alertWarning(jsonResult.msg);
                }
            },
            error: function (data) {
                $.modal.alertWarning("退款失败");
            }
        });
    }

    /**
     * 提交退换货审核
     */
    function submitAudit() {
        var aftersalesId = $("#aftersalesId").val();
        var aftersalesType = $("input[name='aftersalesType']:checked").val();
        var auditType = $("input[name='auditType']:checked").val();
        var remark = $("#remark").val();
        // 调用接口
        $.ajax({
            type: "POST",
            url: ctx + "bill/aftersales/audit",
            // contentType: "application/json", //如果提交的是json数据类型，则必须有此参数,表示提交的数据类型
            //dataType: "json", //表示返回值类型，不必须
            data: {
                "aftersalesId": aftersalesId,
                "aftersalesType": aftersalesType,
                "auditType": auditType,
                "remark": remark
            },
            datatype: "json",
            success: function (jsonResult) {
                if (jsonResult.code == 0) {
                    // 审核成功
                    $.modal.alertSuccess("审核成功");
                    $("#auditModal").hide();
                    // 刷新
                    location.reload();
                } else {
                    $.modal.alertWarning(jsonResult.msg);
                }
            },
            error: function (data) {
                $.modal.alertWarning("审核失败");
            }
        });
    }
</script>
</body>
</html>